layui.use(['form','table','element','laytpl','layer','upload','laydate'], function() {
  var table = layui.table;
  var form = layui.form;
  var laydate = layui.laydate;
  var upload = layui.upload;
  table.render({
    elem: '#supplierTable'
    ,url:'/ssm/product/findMyProduct.do'
    ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
    ,page : true
    ,height : "auto"
    ,cols: [[
      {field:'pid', width:80, title: 'ID',type:'numbers'}
      ,{field:'pname', title: '商品名称'}
      ,{field:'cname',  title: '分类'}
      ,{field:'estoreprice', title: '商城价'}
      ,{field:'markprice',  title: '市场价'}
      ,{field:'pnum', title: '当前库存'}
      ,{field: 'status', title: '上架状态', width:150, align:"center",templet : '#statusTpl'}
      ,{align:'center', toolbar: '#barTpl', width: 120, title: '操作'}
    ]]
  });
  

  //工具条点击事件
  	layui.table.on('tool(table)', function(obj){
  		var data = obj.data;
  		var layEvent = obj.event;

  		if(layEvent === 'edit'){ //修改
  			showEditModel(data);
  		} else if(layEvent === 'del'){ //删除
  			doDelete(data);
  		} else if(layEvent == 'detail'){
  			showPermDialog(data);
  		}else if(layEvent == 'operateAuth'){
  			perAuthWin(data.id,data.role_name);
  		} else if(layEvent === 'send'){ //删除
  			send(data);
  		}
  	});

  	layui.form.on('switch(isDisableServiceCB)', function(obj) {
        updateDisableService(obj);
   });

  	function updateDisableService(obj) {
        var newStatus = obj.elem.checked ? 1 : 0;
        $.post("/ssm/product/updateStatus.do", {
            id : obj.elem.value,
            status : newStatus,
        }, function(data) {
            layer.closeAll('loading');
            if (data.code == '01') {
                //layui.table.reload('table', {});
            } else {
                layer.msg(data.message, {
                    icon : 2
                });
            }
        });
    }
  	
  	//添加按钮点击事件
  	$("#addBtn").click(function(){
  		showEditModel(null);
  		return false;
  	});

  	//显示表单弹窗
    function showEditModel(data){
        layer.open({
            type: 2,
            title: data==null?"添加":"修改",
    		area:['60%','80%'],
            offset: 'auto',
            content: '/ssm/consumer/addProduct.html?id='+(data==null?'':data.pid),
            success:function(){
               
           }
        });
    }

    
	//删除
	function doDelete(data){
        layer.confirm('确定删除该商品吗？',{icon:3, title:'提示信息'},function(index){
            $.post("/ssm/product/del.do",{
            		id : data.pid
            },function(data){
                if(data.code=="01"){
                	layer.close(index);
                	layui.table.reload('supplierTable', {});
                    
                }else{
                	layer.close(index);
                    layer.alert("删除失败");
                    
                }

            })
        });
	}
	
	function loadAllclasses(){
		var postData = {
				notPid :0
		}
        $.get('/ssm/classes/queryList.do',postData,function(res){
                data= res.body;
                for(var idx in data){
                     var optionElement = document.createElement("option");
                     optionElement.setAttribute("value",data[idx].cid);
                     optionElement.text = (data[idx].cname);
                    document.getElementById("cid").append(optionElement);
                 }
                form.render('select');
            })
    }
	
	function send(data){
        layer.confirm('是否确认发货？',{icon:3, title:'提示信息'},function(index){
            $.post("/ssm/order/updateStatus.do",{
            		oid : data.oid,
            		state:2,
            },function(data){
                if(data.code=="01"){
                	layer.close(index);
                	layui.table.reload('supplierTable2', {});
                    
                }else{
                	layer.close(index);
                    layer.alert(data.body);
                    
                }

            })
        });
	}
	
	 function showPermDialog(data){
    	 layer.open({
             type: 1,
             title: data=="详情",
             area: '90%',
             offset: 'auto',
             content: $("#detailModel").html()
         });
    	 
         if(data!=null){
        	 table.render({
     		    elem: '#supplierTable3'
     		    ,url:'/ssm/order/queryOrderProduct.do'
     		    ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
     		    ,page : false
     		    ,where:{
     		    	oid:data.oid
     		    }
     		    ,cols: [[
     		    	{field:'pname', title: '商品名称'}
     		       ,{field:'estoreprice', title: '单价'}
     		      ,{field:'buynum', title: '购买数量'}
     		    ]]
     		  });
         }
         $("#btnCancel").click(function(){
             layer.closeAll('page');
         });
    }

});